WEBVTT

00:00.940 --> 00:06.400
In this video you will learn how to merge time series and real world it is often the case that yes for

00:06.400 --> 00:13.240
example historical stock prices for Apple and one data frame and prices for Boeing in another data frame.

00:13.240 --> 00:18.880
And typically the periods are the time stamps are different for both data frames and then you want to

00:18.880 --> 00:21.930
join both data frames into one data frame.

00:22.120 --> 00:27.550
And even if the Axis advance the panda's methods to join and merge data frames.

00:27.650 --> 00:34.240
That's a pretty pragmatic and straightforward way to do this with the basic tools that we have already

00:34.240 --> 00:41.800
learned and the first of all we import pandas then we import the CSP five stocks and we only select

00:42.010 --> 00:44.440
the closing prices.

00:44.440 --> 00:50.560
So here we have our six stocks and then let's assume that we have Apple stock prices then one data frame

00:50.560 --> 00:53.770
for the period to tend to 14.

00:54.100 --> 00:56.500
So that's the apple data frame.

00:56.500 --> 01:04.580
And uh then in a separate data frame we have stock prices for Boeing for 2 12 to 16.

01:04.660 --> 01:08.220
So that's uh the Boeing data frame.

01:08.220 --> 01:14.650
And let's assume that we want to work with the apple prices and Boeing prices and compare them and work

01:14.650 --> 01:22.180
with them together and we can actually simply do this by creating for example in our apple data frame

01:22.210 --> 01:31.960
a new column Boeing and we assign uh the Boeing stock prices or let's do this here and have a look and

01:31.960 --> 01:39.790
here to 10 we are starting with missing values because our Boeing data frame only starts in 2012 and

01:39.790 --> 01:48.160
it's also if I look here at the last timestamps in 214 and here we have the Boeing prices the to 14

01:49.490 --> 01:55.640
and actually even copying here the Boeing prices into our Apple data frame so penned us automatically

01:55.660 --> 02:00.550
aligns the the prices here corresponding to the timestamps.

02:00.860 --> 02:07.130
So each and every price here will be at the identical timestamp here and the apple data frame than before

02:07.130 --> 02:08.620
in the Boeing data frame.

02:09.870 --> 02:14.580
So this works pretty well and there is actually nothing we have to care about and there's actually no

02:14.580 --> 02:18.280
need for specialized the merging or joining methods.

02:18.420 --> 02:21.740
So that's uh pretty cool and simple here.

02:21.870 --> 02:27.120
And of course if we are only interested in the timestamps uh where we have uh data for Apple and for

02:27.120 --> 02:34.530
Boeing then we can drop here the rows with at least the one missing value with the drop and a method.

02:34.530 --> 02:41.300
So here we have the data from 212 to 214 and actually both of the re indexed method.

02:41.310 --> 02:47.820
We can also align the Boeing data frame on the timestamps that we have in the Apple data frame so we

02:47.820 --> 02:56.910
use the array index method on the Boeing data frame and pass the day time index of the apple data frame.

02:56.930 --> 02:59.050
And if we get actually the same result.

02:59.050 --> 03:05.850
So we have the timestamps that we have ended up with data frame and the corresponding Boeing prices

03:08.660 --> 03:14.240
and let's assume we have a set time series with historical stock prices for this.

03:14.990 --> 03:23.600
And we have them for the period from 210 to 216 and we have actually a weekly date of this Fridays so

03:23.600 --> 03:33.080
let's have a look at the distance data frame from 2010 to 2016 and the weekly price data and let's recall

03:33.080 --> 03:34.800
our Apple data frame.

03:34.820 --> 03:37.120
So here we have daily data.

03:38.070 --> 03:44.520
And also here we can't read the additional column with the Disney prices and we can assign.

03:44.670 --> 03:49.410
The Disney column off the Disney radar frame.

03:49.410 --> 03:57.620
So here we have Disney prices but only for Fridays and otherwise we have missing values and also here

03:57.620 --> 04:06.180
we can re index the Disney data frame for the day time index that we have in the Apple data frame and

04:06.180 --> 04:13.180
also here we have only prices soar values on Fridays and typically when merging time series serve as

04:13.210 --> 04:14.900
a different frequency.

04:14.920 --> 04:22.180
It definitely makes sense to align the data based on the time series with a lower frequency.

04:22.500 --> 04:29.810
So here there is no time series and therefore we can also create here the additional column appear with

04:29.820 --> 04:38.990
the the apple prices and we have actually our weekly daytime index with the weekly prices for Disney

04:39.020 --> 04:39.940
and for Apple.

04:40.400 --> 04:47.750
So also here the apple prices that we copied from the Apple data frame are automatically aligned to

04:47.750 --> 04:49.170
the daytime index.

04:49.190 --> 04:54.620
And what this new data frame and that's actually nothing to care or two very here.

04:54.620 --> 04:57.880
So this works pretty well and automatically.

04:58.220 --> 05:02.120
And with this we are finished here and I hope to see you also in the next video by.
